package com.fengniaoxls.fengniaonewretail.jpush.voice.service;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.fengniaoxls.fengniaonewretail.app.App;
import com.fengniaoxls.fengniaonewretail.commom.constants.Constants;
import com.fengniaoxls.fengniaonewretail.utils.SPUtils;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;



/**
 * @author Freak
 * @date 2019/7/24.
 */
public class PixelActivity extends Activity {
    /**
     * 定时任务
     */
    private ScheduledThreadPoolExecutor scheduled;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("TAG", "锁屏开启一像素activity");
        if (scheduled == null) {
            Log.e("TAG", "锁屏定时任务");
            scheduled = new ScheduledThreadPoolExecutor(2);
        }
        timingTask();
        Window window = getWindow();
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.height = 1;
        params.width = 1;
        //设置全屏不可操作，也就是触摸失效
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        //取消全屏不可触摸
//        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        window.setAttributes(params);

        KeepLiveManager.getInstance().setKeepLiveActivity(this);
    }

    public void timingTask() {
        if ((boolean) SPUtils.get(App.getInstance().getApplicationContext(),
                (String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.USER_CODE, "")
                        + Constants.VOICE_OPEN_STATUS, false)) {
            //延时1s，每隔300000(5分钟)毫秒执行一次run方法
            if (scheduled != null) {
                /**
                 * 0表示首次执行任务的延迟时间，5表示每次执行任务的间隔时间，TimeUnit.MINUTES执行的时间间隔数值单位
                 * 间隔单位毫秒：TimeUnit.MILLISECONDS
                 * 间隔单位秒：TimeUnit.SECONDS
                 * 间隔单位分钟：TimeUnit.MINUTES
                 * 间隔单位小时：TimeUnit.HOURS
                 * 间隔单位天：TimeUnit.DAYS
                 * scheduleAtFixedRate和scheduleWithFixedDelay的区别：
                 * scheduleAtFixedRate：是上一个任务开始执行之后延迟设定时间再执行，是从上一个任务开始时计时，
                 *                      但对于运行时长超过延迟时长的任务，会等上一个任务执行完之后，下一个任务才开始执行，
                 *                      此时，延时没有任何意义。
                 * scheduleWithFixedDelay：scheduleWithFixedDelay是在上一个任务结束执行之后延迟设定时间再执行，
                 *                          是从上一个任务结束时开始计算。
                 */
                scheduled.scheduleWithFixedDelay(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    }
                }, 0, 3, TimeUnit.SECONDS);
            }
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                getJPushStatus();
            }
            super.handleMessage(msg);
        }
    };

    public void getJPushStatus() {
//        if (!JPushInterface.getConnectionState(App.getInstance().getApplicationContext())) {
//            Log.e("TAG", "锁屏    连接过时");
//            System.out.println("锁屏    连接过时");
//            JPushInterface.resumePush(App.getInstance().getApplicationContext());
//        } else {
//            Log.e("TAG", "锁屏   连接正常");
//            System.out.println("锁屏    连接正常");
//        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (scheduled != null) {
            if (!scheduled.isShutdown()) {
                scheduled.shutdown();
            }
            scheduled = null;
        }

    }
}